﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Dukous.Security.Domain
{
    /// <summary>
    /// 用户
    /// </summary>
    [Serializable]
    public class User : DomainEntity
    {
        /// <summary>
        /// 获取 / 设置 用户名称
        /// </summary>
        public virtual string UserName { get; set; }

        /// <summary>
        /// 获取 / 设置 密码
        /// </summary>
        public virtual string Password { get; set; }

        /// <summary>
        /// 获取 / 设置 是否禁用
        /// </summary>
        public virtual bool Disabled { get; set; }

        /// <summary>
        /// 获取 / 设置 组
        /// </summary>
        public virtual IList<Group> Groups { get; set; }

        /// <summary>
        /// 获取 / 设置 
        /// </summary>
        public virtual IList<SecurityObjectPermission> SecurityObjectPermissions { get; set; }

        /// <summary>
        /// 添加一个新角色（会自动在 Role 中建立相应关联）
        /// </summary>
        /// <param name="group"></param>
        public virtual void AddGroup(Group group)
        {
            InternalAddGroup(group);
            group.InternalAddUser(this);
        }

        /// <summary>
        /// 移除一个新角色（会自动在 Role 中删除相应关联）
        /// </summary>
        /// <param name="group"></param>
        public virtual void RemoveGroup(Group group)
        {
            InternalRemoveGroup(group);
            group.InternalRemoveUser(this);
        }

        /// <summary>
        /// 添加一个新角色
        /// </summary>
        /// <param name="group"></param>
        protected internal virtual void InternalAddGroup(Group group)
        {
            //Check.Require(group != null);

            if (this.Groups == null)
            {
                this.Groups = new List<Group>();
            }
            this.Groups.Add(group);
        }

        /// <summary>
        /// 移除一个新角色
        /// </summary>
        /// <param name="group"></param>
        protected internal virtual void InternalRemoveGroup(Group group)
        {
            //Check.Require(group != null);

            if (this.Groups == null)
            {
                this.Groups = new List<Group>();
            }
            this.Groups.Remove(group);
        }
    }
}
